package servlet;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import util.DButil;
import util.staff;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Select extends HttpServlet{

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        String selectby = request.getParameter("selectBy");
        String keyword = request.getParameter("keyword");
        String close = request.getParameter("close");

        String sql = "select * from staff";

        if(keyword != null && keyword !=""){
            if(selectby.equals("ID")){
                int keyword_1 = Integer.parseInt(keyword);
                if(close != "" && close != null)
                    sql = "select * from staff where id='"+ keyword_1 +"'";
                else
                    sql = "select * from staff where id like '%"+keyword_1+"%'";
            }
            if(selectby.equals("姓名")){
                if(close != "" && close != null)
                    sql = "select * from staff where name='"+ keyword +"'";
                else
                    sql = "select * from staff where name like '%"+keyword+"%'";
            }
        }


        try {
            DButil dButil = new DButil();
            Connection con = dButil.getConnection();

            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(sql);

            String sqll = "INSERT INTO result VALUES (?,?,?,?,?,?,?,?,?,?)";

            PreparedStatement ps = con.prepareStatement(sqll);

            while(rs.next()) {
                staff staff = new staff();
                staff.setId(rs.getString("id"));
                staff.setName(rs.getString("name"));
                staff.setWage(rs.getDouble("wage"));
                staff.setWyear(rs.getDouble("wyear"));
                staff.setTel(rs.getString("tel"));
                staff.setAddress(rs.getString("address"));
                staff.setOrigin(rs.getString("origin"));
                staff.setDegree(rs.getString("degree"));
                staff.setBirth(rs.getString("birth"));
                staff.setSex(rs.getString("sex"));

//                list.Insert(staff);
//                n++;
                ps.setString(1,staff.getId());
                ps.setString(2,staff.getName());
                ps.setString(3,staff.getSex());
                ps.setString(4,staff.getBirth());
                ps.setString(5,staff.getDegree());
                ps.setString(6,staff.getOrigin());
                ps.setString(7,staff.getAddress());
                ps.setString(8,staff.getTel());
                ps.setDouble(9,staff.getWyear());
                ps.setDouble(10,staff.getWage());

                ps.executeUpdate();
            }

/*

            //将数据库中查询到的信息封装在user对象中，use对象保存在request中，之后将通过request对象将数据传递到页面
            //需要的话也可以只传递一个参数request.setAttribute("id", user.getId());
            request.setAttribute("List", list);
            //这里是转发，从Servlet跳转到showinfo.jsp页面，并且带上request和response对象中原有的参数
            request.getRequestDispatcher("showselect.jsp").forward(request, response);
*/
        } catch (SQLException e) {

        }


        RequestDispatcher dispatcher=
                request.getRequestDispatcher("result.jsp");//转发
        dispatcher.forward(request,response);
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}